package cn.hx.mallBlog.controller;

import cn.hx.mallBlog.entity.CmsTopicComment;
import cn.hx.mallBlog.entity.UmsAdmin;
import cn.hx.mallBlog.entity.UmsMember;
import cn.hx.mallBlog.service.CmsTopicCommentService;
import cn.hx.mallBlog.service.UmsAdminService;
import cn.hx.mallBlog.service.UmsMemberService;
import cn.hx.mallBlog.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 专题评论表(CmsTopicComment)表控制层
 *
 * @author makejava
 * @since 2022-11-30 15:38:59
 */
@Controller
public class CmsTopicCommentController {
    /**
     * 服务对象
     */
    @Resource
    private CmsTopicCommentService cmsTopicCommentService;
    @Resource
    private UmsMemberService umsMemberService;

    @PostMapping("addToic")
    @ResponseBody
    public String addToic(String content, HttpSession session, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Long id=0L;
        for (Cookie cookie : cookies) {
            if ("token".equals(cookie.getName())) {
                String token = cookie.getValue();
                Claims claims = JwtUtils.getClaims(token);
                id = Long.valueOf(claims.get("data", Map.class).get("id").toString());
                System.out.println("当前id==>" + id);
            }
        }
//        if ("token".equals(cookies)&&cookies.length>0){
//            for (Cookie cookie : cookies) {
//                String name = cookie.getName();
//                String value = cookie.getValue();
//                if (JwtUtils.verifyToken(value)){
//                    Claims claims = JwtUtils.getClaims(value);
//                     id = Long.valueOf(claims.get("data", Map.class).get("id").toString());
//                    System.out.println(id);
//                }
//            }
//        }
        System.out.println("这是一个addToic"+id);
        UmsMember umsAdmin = umsMemberService.queryById(id);
        CmsTopicComment cmsTopicComment = new CmsTopicComment();
        cmsTopicComment.setContent(content);
        cmsTopicComment.setCreateTime(new Date());
        cmsTopicComment.setMemberIcon(umsAdmin.getIcon());
        long toicId = Long.valueOf(session.getAttribute("id").toString());
        cmsTopicComment.setTopicId(toicId);
        cmsTopicComment.setMemberNickName(umsAdmin.getNickname());
        cmsTopicComment.setShowStatus(1);
        cmsTopicComment.setViewId(1);
        cmsTopicCommentService.insert(cmsTopicComment);
        return "评论成功！！！";
    }

    @PostMapping("checkToic")
    @ResponseBody
    public List<CmsTopicComment> check(String id,HttpSession session) {
        CmsTopicComment cmsTopicComment = new CmsTopicComment();
        cmsTopicComment.setViewId(Integer.valueOf(id));
        cmsTopicComment.setTopicId(Long.valueOf(session.getAttribute("id").toString()));
        List<CmsTopicComment> list = cmsTopicCommentService.queryAll(cmsTopicComment);
        return list;
    }

    @PostMapping("addToicAndTwo")
    @ResponseBody
    public String addToicAndTwo(String content,String viewId,HttpSession session,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Long id=0L;
        for (Cookie cookie : cookies) {
            if ("token".equals(cookie.getName())) {
                String token = cookie.getValue();
                Claims claims = JwtUtils.getClaims(token);
                id = Long.valueOf(claims.get("data", Map.class).get("id").toString());
                System.out.println("当前id==>" + id);
            }
        }
//        if ("token".equals(cookies)&&cookies.length>0){
//            for (Cookie cookie : cookies) {
//                String name = cookie.getName();
//                String value = cookie.getValue();
//                if (JwtUtils.verifyToken(value)){
//                    Claims claims = JwtUtils.getClaims(value);
//                    id = Long.valueOf(claims.get("data", Map.class).get("id").toString());
//                    System.out.println(id);
//                }
//            }
//        }
        System.out.println("这是一个addToicAndTwo"+id);
        UmsMember umsAdmin = umsMemberService.queryById(id);
        CmsTopicComment cmsTopicComment = new CmsTopicComment();
        cmsTopicComment.setContent(content);
        cmsTopicComment.setCreateTime(new Date());
        cmsTopicComment.setMemberIcon(umsAdmin.getIcon());
        long toicId = Long.valueOf(session.getAttribute("id").toString());
        cmsTopicComment.setTopicId(toicId);
        cmsTopicComment.setMemberNickName(umsAdmin.getNickname());
        cmsTopicComment.setShowStatus(2);
        cmsTopicComment.setViewId(Integer.valueOf(viewId));
        cmsTopicCommentService.insert(cmsTopicComment);
        return "回复成功";
    }
}